<html>
<head><meta charset="utf-8"><title>macros, write!, and {fmt, io}::Write · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html">macros, write!, and {fmt, io}::Write</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209200090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209200090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209200090">(Sep 05 2020 at 23:25)</a>:</h4>
<p>Definitely feel like I'm in "asking obvious questions" territory, but I'm looking at one of the older issues re: <code>write!</code> slowness, <a href="https://github.com/rust-lang/rust/issues/10761">issue 10761</a>, and part of the slowness seems to be that it can't simply directly call <code>io::Write::write{_all}</code> when it would be faster to. One of the ways it could be handled is that the macro could be altered to branch on literals, but I still have to choose between using <code>write_str</code> or <code>write</code>, which are not viable if I need to service both <code>fmt::Write</code> and <code>io::Write</code>, right? <del>It seems like if I call <code>write_fmt</code>, I would just get the same overhead I currently do (though I should test that more thoroughly).</del> Answer: yes.</p>
<p>So either io::Write needs to somehow grow <code>write_str</code> or fmt::Write needs to grow <code>write</code> to take the "obvious" solution of "just special case literals" or I need to be able to detect what traits are applicable and in scope (lol glwt) but that seems to be invalid due to macro hygiene?</p>



<a name="209207788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209207788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209207788">(Sep 06 2020 at 04:01)</a>:</h4>
<p>some other options:<br>
1) call a function that uses trait-bounds + specialization to apply a valid <code>.write{_str}( )</code> call<br>
2) specialize write_fmt itself on fmt::Write and io::Write??<br>
3) reexamine <code>format_args!</code> for speed</p>



<a name="209221659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209221659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209221659">(Sep 06 2020 at 11:44)</a>:</h4>
<p>Cc <span class="user-mention" data-user-id="209117">@lzutao</span> who was doing some work in this area</p>



<a name="209221702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209221702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209221702">(Sep 06 2020 at 11:44)</a>:</h4>
<p>(although he self-described it as a hack so maybe he'd prefer your approach :P)</p>



<a name="209224989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209224989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DPC <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209224989">(Sep 06 2020 at 13:10)</a>:</h4>
<p>"dec 2013" <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span></p>



<a name="209226664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209226664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209226664">(Sep 06 2020 at 14:01)</a>:</h4>
<p><span class="user-mention" data-user-id="281757">@Jubilee</span> sound good, do you want to work on this issue or you need collaboration ?</p>



<a name="209243296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209243296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209243296">(Sep 06 2020 at 21:55)</a>:</h4>
<p>I'm still a bit in the "trying to absorb ideas for how it might be fixed" stage, so~ Either way~? <span aria-label="on" class="emoji emoji-1f51b" role="img" title="on">:on:</span><br>
Complication is that the macro itself can't special case an arbitrary number of literals, only one, because positional args are injected into string literals. But if there's only one literal there will never be a second positional arg, so as long as I don't break the error reporting for an unused positional arg, that might be a notable gain still, since I am guessing <em>many</em> write! invocations are single literal.</p>
<p>Unless I'm misunderstanding something and somehow the positional args are not in fact string literals, but that seems unlikely.<br>
...oh wait, implicit format args breaks this assumption. <a href="https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html">https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html</a></p>



<a name="209251710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209251710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209251710">(Sep 07 2020 at 02:17)</a>:</h4>
<blockquote>
<p>This RFC argues that format_args! should not attempt to expand any implicit named arguments if the macro is provided with an expression instead of a verbatim string literal.</p>
</blockquote>
<p>Oh, I could at least lean on that, I suppose.</p>



<a name="209253760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209253760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209253760">(Sep 07 2020 at 03:19)</a>:</h4>
<p>Though, I could also have the literal statically verified by macro invocation or const fn to be of a kind that does not contain a potential positional arg, and then compile to format_args and write_fmt if it needs it. So the fast path is actually doable inside compilation time. Yeah, I think special-casing single literals and single exprs is actually looking pretty viable.</p>



<a name="209256768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209256768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209256768">(Sep 07 2020 at 04:44)</a>:</h4>
<div class="codehilite"><pre><span></span><code>running 10 tests
test string_write_fmt    ... bench:      10,053 ns/iter (+/- 1,141)
test string_write_macro1 ... bench:      10,177 ns/iter (+/- 2,363)
test string_write_macro2 ... bench:      17,499 ns/iter (+/- 1,847)
test string_write_ref    ... bench:       2,270 ns/iter (+/- 265)
test string_write_value  ... bench:       2,333 ns/iter (+/- 126)
test vec_write_fmt       ... bench:      15,722 ns/iter (+/- 1,673)
test vec_write_macro1    ... bench:      15,767 ns/iter (+/- 1,638)
test vec_write_macro2    ... bench:      23,968 ns/iter (+/- 8,942)
test vec_write_ref       ... bench:       2,296 ns/iter (+/- 178)
test vec_write_value     ... bench:       2,230 ns/iter (+/- 235)

test result: ok. 0 passed; 0 failed; 0 ignored; 10 measured; 0 filtered out
</code></pre></div>


<p>What's really funny is how writing "abc" to Vec&lt;u8&gt; has even more overhead than writing to a String, even though they go through the same(?) formatting machinery.</p>



<a name="209331262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209331262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209331262">(Sep 07 2020 at 22:29)</a>:</h4>
<p>I am no longer surprised:</p>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="k">fn</span> <span class="nf">write_fmt</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">fmt</span>: <span class="nc">fmt</span>::<span class="n">Arguments</span><span class="o">&lt;</span><span class="nb">&#39;_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// Create a shim which translates an io::Write to a fmt::Write and saves</span>
<span class="w">        </span><span class="c1">// off I/O errors. instead of discarding them</span>
<span class="w">        </span><span class="k">struct</span> <span class="nc">Adaptor</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="o">?</span><span class="nb">Sized</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">inner</span>: <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">error</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>

<span class="w">        </span><span class="cm">/* More code related to implementing and using the resulting shim, at runtime??? */</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209341790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209341790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209341790">(Sep 08 2020 at 03:02)</a>:</h4>
<p>I notice a <strong>lot</strong> of related issues. Is it okay to compile them into a new metabug issue?</p>



<a name="209341862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209341862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209341862">(Sep 08 2020 at 03:04)</a>:</h4>
<p>closing things as duplicates sounds fine to me</p>



<a name="209341871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209341871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209341871">(Sep 08 2020 at 03:05)</a>:</h4>
<p>if you link me some issues I can close them</p>



<a name="209341881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209341881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209341881">(Sep 08 2020 at 03:05)</a>:</h4>
<p>(after glancing to make sure they're actually duplicates ;)</p>



<a name="209341949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209341949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209341949">(Sep 08 2020 at 03:07)</a>:</h4>
<p>Oh, I was thinking of filing a bug that takes and restates the issue so as to safely mark the rest as closable duplicates of that bug, because there's a few different niches of it that need to be pulled together and seen together.</p>



<a name="209434160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209434160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209434160">(Sep 08 2020 at 19:40)</a>:</h4>
<p>You can proooobably close a few of the linked issues from here: <a href="https://github.com/rust-lang/rust/issues/76490">https://github.com/rust-lang/rust/issues/76490</a><br>
^_^</p>



<a name="209434703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209434703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209434703">(Sep 08 2020 at 19:45)</a>:</h4>
<p>Certainly the recent ones that basically restate <a href="https://github.com/rust-lang/rust/issues/10761">#10761</a>.</p>



<a name="209461636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macros%2C%20write%21%2C%20and%20%7Bfmt%2C%20io%7D%3A%3AWrite/near/209461636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DPC <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macros.2C.20write!.2C.20and.20.7Bfmt.2C.20io.7D.3A.3AWrite.html#209461636">(Sep 09 2020 at 00:52)</a>:</h4>
<p>i wouldn't close them yet</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>